package com.shm.leetcode.concurrent;

import java.util.concurrent.locks.ReentrantLock;
/**
 * @author: shm
 * @dateTime: 2020/10/16 18:41
 * @description: “只允许1个哲学家就餐”是严格的“串行”
 */
class DiningPhilosophers2 {

    public DiningPhilosophers2() {
        
    }
    private ReentrantLock lock = new ReentrantLock();
    // call the run() method of any runnable to execute its code
    public void wantsToEat(int philosopher,
                           Runnable pickLeftFork,
                           Runnable pickRightFork,
                           Runnable eat,
                           Runnable putLeftFork,
                           Runnable putRightFork) throws InterruptedException {
        lock.lock();
        try{
        pickLeftFork.run();
        pickRightFork.run();
        eat.run();
        putLeftFork.run();
        putRightFork.run();
        }finally{
            lock.unlock();
        }
    }
}